﻿using System.Collections.Generic;
using System.Runtime.Serialization;
using GMME.Util;
using UnityEngine;
using UnityEngine.Scripting;

namespace GMME.Model.Rtm.Result
{
    [DataContract]
    public class GetRtmChannelPlayerPropertiesResult : ErrorResult
    {
        /**
         * 开发者传入的频道Id
         */
        [DataMember(Name = "channelId")]
        [Preserve]
        public string ChannelId { get; set; }

        /**
         * Rtm频道内玩家信息
         */
        [DataMember(Name = "memberInfos")]
        [Preserve]
        public List<RtmChannelMemberInfo> MemberInfos { get; set; }

#if UNITY_ANDROID
        internal static GetRtmChannelPlayerPropertiesResult ConvertGetRtmChannelPlayerPropertiesResult(
            AndroidJavaObject javaObject)
        {
            if (javaObject != null)
            {
                var result = new GetRtmChannelPlayerPropertiesResult();
                result.ChannelId = javaObject.Call<string>("getChannelId");
                result.Code = javaObject.Call<int>("getCode");
                result.Msg = javaObject.Call<string>("getMsg");
                result.MemberInfos = new List<RtmChannelMemberInfo>();
                var list =
                    TransferUtil.JavaListToCsharpList(
                        javaObject.Call<AndroidJavaObject>("getMemberInfos"));
                foreach (var element in list)
                {
                    var rtmChannelMemberInfo =
                        RtmChannelMemberInfo.ConvertRtmChannelMemberInfo(element);
                    result.MemberInfos.Add(rtmChannelMemberInfo);
                }

                return result;
            }

            return null;
        }
#endif


        public void SetPlayerProperties()
        {
            if (MemberInfos != null)
                foreach (var memberInfo in MemberInfos)
                    memberInfo.ConvertPropertiesToDict();
        }
    }
}